# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql

class KuaishouPipeline(object):
    def open_spider(self, spider):
        self.con = pymysql.connect('127.0.0.1', 'root', '123456', 'meitu', charset="utf8")
        self.cursor = self.con.cursor()

    def process_item(self, item, spider):
        item["referer"] = spider.name
        sql, data = item.get_sql()
        try:
            self.cursor.execute(sql, data)
            self.con.commit()
            print(data)
        except Exception as e:
            print(e)
            self.con.rollback()
        return item

    def close_spider(self, spider):
        self.cursor.close()
        self.con.close()
